package com.tth.learn.addproperties.java1;

import org.junit.Test;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 实现TCP网络编程
 *
 * 例子1.客户端发送信息给服务端，服务端将接收到的数据显示在控制台
 * @Author tth
 * @Date 2022/12/28 11:22
 */
public class TCPTest1 {

    /**
     * 客户端
     */
    @Test
    public void client() {
        Socket socket = null;
        OutputStream os = null;
        try {
            // 1.创建Socket对象，指明服务器端的IP和端口号
            String serverIp = "127.0.0.1";
            int serverPort = 8899;
            InetAddress serverInet = InetAddress.getByName(serverIp);
            socket = new Socket(serverInet, serverPort);

            // 2.获取一个输出流，用于输出数据
            os = socket.getOutputStream();
            // 3.输出数据
            os.write("你好，我是客户端".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 4.关闭资源
            if (null != os) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (null != socket) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }

    /**
     * 服务端
     */
    @Test
    public void server() {
        ServerSocket ss = null;
        Socket socket = null;
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        try {
            // 1.创建服务端Socket-ServerSocket，指明自己的端口号
            ss = new ServerSocket(8899);
            // 2.调用accept()，接收来自于客户端的Socket
            socket = ss.accept();
            // 3.获取输入流，用于接收数据
            is = socket.getInputStream();

            // 4.读取输入流中的数据
            // 方式一：不建议使用，可能会有乱码
//        byte[] buff = new byte[1024];
//        int len;
//        while ((len = is.read(buff)) != -1) {
//            String str = new String(buff, 0, len);
//            System.out.println(str);
//        }

            // 方式二：推荐
            baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[5];
            int len;
            while ((len = is.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }

            System.out.println(baos.toString());
            System.out.println("收到了来自于"+socket.getInetAddress().getHostAddress()+"的数据");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 5.关闭资源
            if (null != baos) {
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (null != socket) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (null != ss) {
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }
}
